/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.ponly.uno4j.core;

import com.sun.star.beans.PropertyValue;
import org.ponly.uno4j.core.UnoBridge;
import org.ponly.uno4j.core.UnoConv;
import org.ponly.uno4j.core.UnoTask;

import java.io.File;
import java.io.OutputStream;

import static org.ponly.uno4j.util.UnoUtils.of;

/**
 * 文档到PDF文件的UNO转换任务
 *
 * @author vacoor
 */
public class DocToPdfTask extends UnoConv implements UnoTask {
    private static final String DESTINATION_EXTENSION = "pdf";
    private static final PropertyValue[] DEFAULT_LOAD_PROPS = of("Hidden", true, "ReadOnly", true);
    private static final PropertyValue[] DEFAULT_STORE_PROPS = of(
            "Hidden", true,
            "Overwrite", true,
            "FilterData", of(
                    // of("PageRange", "1"/*string*/),      // 虽然可以范围转, 但是加载文档太耗时,效率太低, 不如一次转完(单load就很耗时)
                    of("SelectPdfVersion", 0),              // 0 = PDF 1.4 (default selection). 1 = PDF/A-1 (ISO 19005-1:2005)
                    of("ExportBookmarks", true),
                    of("ExportNotesPages", false),          // 否则会将所有有note的在下面重新导出一分
                    of("DisplayPDFDocumentTitle", false)
            )
    );

    protected final File source;
    protected final OutputStream destination;

    /**
     * 之所以输入参数为 File 是因为实际测试中发现, UNO 使用 URL 居然比 InputStream 效率高很很多
     */
    public DocToPdfTask(File source, OutputStream destination) {
        super(DESTINATION_EXTENSION);
        this.source = source;
        this.destination = destination;
    }

    @Override
    public void process(UnoBridge unoBridge) throws Exception {
        convert(unoBridge, source, DEFAULT_LOAD_PROPS, destination, DEFAULT_STORE_PROPS);
    }
}
